﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Task
{
    public partial class Help : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.btnDelete.Attributes["autopostback"] == null)
                this.btnDelete.Attributes.Add("autopostback", "false");

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            if (!Page.IsPostBack)
            {
                bool isManager = ReportHelper.IsAdmin(user.EmployeeId);
                Session["isManager"] = isManager;
                if (!isManager)
                {
                    this.divManager.Visible = false;
                    this.btnDelete.Text = "取消 (Cancel)";
                    this.btnDelete.Attributes.Add("onClick","window.close()");
                }

                else
                {
                    this.btnDelete.Attributes.Add("onClick", "OnRptDelete();");
                }

                this.GetData();
            }
        }

        protected void GetData( )
        {
            this.gvHelp.Columns[0].Visible = Convert.ToBoolean(Session["isManager"].ToString());

            BEGDC.PreContract.BLL.IHelpDoc iHelpDoc = (BEGDC.PreContract.BLL.IHelpDoc)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_HelpDoc_Service];

            DataTable dt = iHelpDoc.GetHelpDoc_DataTable("1=1 ORDER BY DateCreated DESC");

            this.gvHelp.DataSource = dt;
            this.gvHelp.DataBind();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (uplFile.HasFile==false)
            {

                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + "您上传的文件不存在.\\nUploaded file not exists." + "')</script>");
                return;
            }

            if (this.txtDescription.Text == null || this.txtDescription.Text.Trim() == String.Empty)
            {

                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + "请输入文件描述.\\nPlease input the description." + "')</script>");
                return;
            }

            if (System.IO.Path.GetExtension(this.uplFile.FileName).ToLower() != ".doc" && System.IO.Path.GetExtension(this.uplFile.FileName).ToLower() != ".ppt" && System.IO.Path.GetExtension(this.uplFile.FileName).ToLower() != ".xls")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + "上传文件的扩展名必须是doc或ppt.\\n\\nThe upload file extension must be doc or ppt." + "')</script>", false);
                return;
            }

            string filePath = BEGDC.MIS.Office.Configuration.UploadPath + uplFile.FileName;

            //Joan 2008-10-27 check if file exists
            if (System.IO.File.Exists(filePath))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + "文件已存在\\nFile already exists." + "')</script>");
                return;           
            
            }

            this.uplFile.SaveAs(filePath);

            try
            {
                BEGDC.PreContract.DAL.HelpDoc helpDoc = new BEGDC.PreContract.DAL.HelpDoc();
                helpDoc.DateCreated = DateTime.Now;
                helpDoc.DocPath = BEGDC.MIS.Office.Configuration.UploadPath_Relative + uplFile.FileName; ;
                helpDoc.DocDescription = this.txtDescription.Text.Trim();
                helpDoc.Create();

                this.GetData();
            }
            catch (Exception ex)
            {

                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                BEGDC.PreContract.BLL.IHelpDoc iHelpDoc = (BEGDC.PreContract.BLL.IHelpDoc)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_HelpDoc_Service];
                CheckBox chkSelect = new CheckBox();
                string filePath = string.Empty;
                BEGDC.PreContract.DAL.HelpDoc helpDoc = new BEGDC.PreContract.DAL.HelpDoc();

                int num = 0;

                for (int i = 0; i < this.gvHelp.Rows.Count; i++)
                {
                    chkSelect = (CheckBox)this.gvHelp.Rows[i].FindControl("chkSelect");

                    if (chkSelect.Checked)
                    {
                        helpDoc = iHelpDoc.GetHelpDoc(Convert.ToInt32(this.gvHelp.DataKeys[i].Value.ToString()));
                        filePath = System.Web.HttpContext.Current.Server.MapPath(helpDoc.DocPath);
                        helpDoc.Delete();
                        if (System.IO.File.Exists(filePath))
                            System.IO.File.Delete(filePath);

                        num++;
                    }
                }

                if (num == 0)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('请选择至少一项!\\n Please select at least one item!')</script>", false);
                    return;
                }

                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('成功删除" + num + "条数据!\\n Deleted " + num + " items successfully!')</script>", false);

                this.GetData();
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript( this.GetType(), "", "<script language='javascript'>alert('" + ex.Message.Replace("'", "\\'") + "')</script>", false);
            }

        }


        //protected void On_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowIndex >= 0)
        //    {
                
        //        if (!System.IO.File.Exists(e.Row.Cells[4].Text))
        //        {
        //            Image img = (Image)e.Row.FindControl("Image1");
        //            if (img != null)
        //                img.Visible = false;
        //        }
               
        //    }
        //}
    }
}
